package com.jlnx.controller.bank;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jlnx.dao.ChainEnterpriseDaoImpl;
import com.jlnx.dao.CoreEnterpriseDaoImpl;
import com.jlnx.dao.EnterpriseDaoImpl;
import com.jlnx.dao.FinanceReqDaoImpl;
import com.jlnx.dao.PayStatusDaoImpl;
import com.jlnx.dao.SupplyChainDaoImpl;
import com.jlnx.pojo.ChainEnterprise;
import com.jlnx.pojo.CoreEnterprise;
import com.jlnx.pojo.Enterprise;
import com.jlnx.pojo.FinanceReq;
import com.jlnx.pojo.SupplyChain;
import com.jlnx.pojo.SupplyChainEnterprise;


@WebServlet("/ShowSupplyChainController")
public class ShowSupplyChainController extends HttpServlet {
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		//获取请求信息
		response.setContentType("application/json;charset=UTF-8;text/html");
		//设置请求编码模式
    	request.setCharacterEncoding("utf-8");
				
		 //获取session对象
//		 HttpSession hs=request.getSession();
//		 //获取登录账号
//		 String bankentNo = ((Enterprise)hs.getAttribute("enterprise")).getEntNo();
//		 System.out.println( bankentNo + ":发起供应链查询请求");
		 
		 SupplyChainDaoImpl supplydao = new SupplyChainDaoImpl();
		 EnterpriseDaoImpl entdao = new EnterpriseDaoImpl();
		 CoreEnterpriseDaoImpl coredao = new CoreEnterpriseDaoImpl();
		 ChainEnterpriseDaoImpl chaindao = new ChainEnterpriseDaoImpl();
		 PayStatusDaoImpl paydao = new PayStatusDaoImpl();
		 
		 //获取全部供应链信息
		List<SupplyChain> ls = supplydao.selectSupplyChainList();
		System.out.println(ls);
		for(SupplyChain supplyChain:ls) {
			//获取供应链企业列表
			String supplyList = supplyChain.getMessage();
			String[] array = supplyList.split("\\|");
			
			List<SupplyChainEnterprise> listsupply = new ArrayList<SupplyChainEnterprise>();
			//循环获取企业相关信息
			for(int i = 0;i < array.length;i++) {
				
				String entNo = array[i];
				System.out.println("entNo:" + entNo);
				
				SupplyChainEnterprise supplyChainEnterprise = null;
				
				int role = entdao.getRoleByEntNo(entNo);
				if(role == 1) {//添加核心企业相关信息
					/********************************************/
					// 为了演示而添加的部分，正常来讲应该通过定时任务进行状态修改，这里改为点击触发
					//查询核心企业是否存在逾期未还的账款
					if(paydao.isOverdueFinanceExist(entNo) > 0) {
						//将企业状态置为不良
						coredao.changeStatus(entNo, 1);
					}else {
						//将企业状态置为良好
						coredao.changeStatus(entNo, 0);
					}
					/********************************************/
					CoreEnterprise coreEnterprise = coredao.selectByEntNo(entNo);
					supplyChainEnterprise = new SupplyChainEnterprise(entNo, coreEnterprise.getName(), coreEnterprise.getInstatus(), coreEnterprise.getStatus(), role);
				}else if(role == 3) {//添加链条企业相关信息
					/********************************************/
					// 为了演示而添加的部分，正常来讲应该通过定时任务进行状态修改，这里改为点击触发
					//查询企业是否存在逾期未还的账款
					if(paydao.isOverdueFinanceExist(entNo) > 0) {
						//将企业状态置为不良
						chaindao.changeStatus(entNo, 1);
					}else {
						//将企业状态置为良好
						chaindao.changeStatus(entNo, 0);
					}
					/********************************************/
					
					ChainEnterprise chainEnterprise = chaindao.selectByEntNo(entNo);
					supplyChainEnterprise = new SupplyChainEnterprise(entNo, chainEnterprise.getName(), chainEnterprise.getInstatus(), chainEnterprise.getStatus(), role);
				}else {
					continue;
				}
				listsupply.add(supplyChainEnterprise);			
			}
			System.out.println("listsupply:" + listsupply);
			supplyChain.setLs(listsupply);
		}
		System.out.println(ls);
		//发送数据到页面
		request.setAttribute("SupplyChainList", ls);
		//跳转界面
		request.getRequestDispatcher("/main/showSupplyChain.jsp").forward(request, response);
				
	}

}
